package org.ihelpuoo.util;

import org.ihelpuoo.io.CharInputStream;
import org.ihelpuoo.lang.LoaderException;
import org.ihelpuoo.lang.StringUtils;
import org.ihelpuoo.parser.segment.CharSegment;
import org.ihelpuoo.parser.segment.Segment;

/**
 * <b>Attribute class:</b>
 * <hr color=grey size=1>
 *
 * Parse one attribute
 *
 * @author zozoh  created @ 2006
 *
 */
public final class Pair
{
	private String				name			= null;
	private Object				value			= null;
	private Segment				pattern			= null;
	private char				segChar			= '=';
	private static final String	pattern_normal	= "${name}=\"${value}\"";
	private static final String	pattern_simple	= "${name}=${value}";
	private static final String	pattern_css		= "${name}:${value};";

	public Pair()
	{
		pattern = new CharSegment();
	}

	public Pair(CharSequence v)
	{
		this();
		valueOf(v);
	}

	public Pair(Pair att)
	{
		if (null != att)
		{
			name = att.getName();
			value = att.getValue();
			segChar = att.getSegChar();
		}
	}

	public Pair(CharSequence v, char segChar)
	{
		this();
		this.segChar = segChar;
		valueOf(v);
	}

	public void valueOf(CharSequence v)
	{
		if (v != null)
		{
			String str = StringUtils.trim(v);
			int index = str.indexOf(segChar);
			if (index >= 0)
			{
				name = str.substring(0,index);
				value = str.substring(index + 1);
				if ((((String)value).startsWith("\"") && ((String)value).endsWith("\""))
						|| ((String)value).startsWith("'") && ((String)value).endsWith("'"))
				{
					value = ((String)value).substring(1,((String)value).length() - 1);
				}
			}
			else
			{
				name = str;
				value = null;
			}
		}
	}

	/**
	 * @return Returns the segChar.
	 */
	public char getSegChar()
	{
		return segChar;
	}

	/**
	 * @param segChar The segChar to set.
	 */
	public void setSegChar(char segChar)
	{
		this.segChar = segChar;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public Object getValue()
	{
		return value;
	}

	public String getValueAsString()
	{
		if (value == null)
			return null;
		return value.toString();
	}

	public void setValue(Object value)
	{
		this.value = value;
	}

	public void useNormalPattern()
	{
		try
		{
			pattern.parse(new CharInputStream(Pair.pattern_normal));
		}
		catch (LoaderException e)
		{
			e.printStackTrace();
		}
	}

	public void useSimplePattern()
	{
		try
		{
			pattern.parse(new CharInputStream(Pair.pattern_simple));
		}
		catch (LoaderException e)
		{
			throw new RuntimeException(e);
		}
	}

	public void useCssPattern()
	{
		try
		{
			pattern.parse(new CharInputStream(Pair.pattern_css));
		}
		catch (LoaderException e)
		{
			e.printStackTrace();
		}
	}

	private Segment getPattern()
	{
		if (null == pattern)
		{
			try
			{
				pattern.parse(new CharInputStream(Pair.pattern_normal));
			}
			catch (LoaderException e)
			{
				throw new RuntimeException(e);
			}
		}
		return pattern;
	}

	public void normalize()
	{
		this.name = StringUtils.trim(this.name);
		if (this.value != null && this.value instanceof CharSequence)
			this.value = StringUtils.trim((CharSequence)this.value);
	}

	@Override
	public String toString()
	{
		getPattern().clearAll();
		getPattern().set("name",name);
		getPattern().set("value",value);
		String re = null;

		re = getPattern().toString();
		return re;
	}
}
